# -*- coding = utf-8 -*-
# @Time : 2021/12/17 15:34
# @Author : 龙王赘婿_彪少
# @File : demo05.py
# @Software: PyCharm

# 重写关闭事件，增加关闭消息提示框
# 将窗口置于屏幕中央


import sys
from PyQt5.QtWidgets import (QWidget, QMessageBox, QApplication, QDesktopWidget)


class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle("消息窗口")
        self.center()
        self.show()

    def closeEvent(self, event):
        reply = QMessageBox.question(self, "消息", "老表你真的要关我么？", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

    def center(self):
        # 获取窗口QRect
        qr = self.frameGeometry()
        # 获取屏幕中心点QPoint,思路是窗口对象->窗口QRect->窗口中心点
        cp = QDesktopWidget().availableGeometry().center()
        # 先把矩形框移动到中心
        qr.moveCenter(cp)
        # 再把窗口移动到矩形框左上角
        self.move(qr.topLeft())
        print(QDesktopWidget().availableGeometry().bottomLeft())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())